//
//  MJTableView.m
//  GoodEit
//
//  Created by ihope99 on 2017/8/10.
//  Copyright © 2017年 GZX. All rights reserved.
//

#import "MJTableView.h"
#import "MJRefresh.h"


@interface MJTableView (){
    BOOL isFooterHiiden;
}
@end

@implementation MJTableView


-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        //自动更改透明度
        self.mj_header.automaticallyChangeAlpha = YES;
        //进入刷新状态
        //        [self.mj_header beginRefreshing];
        self.showHeader = YES;
        self.showFooter = YES;
        self.cacheHeightDict = [NSMutableDictionary dictionary];



    }
    return self;
}
-(void)beginRefreshing{
    [self.mj_header beginRefreshing];
}
-(void)reloadData{
    [self endRefresh];
    [super reloadData];
}
-(void)setShowHeader:(BOOL)showHeader{
    _showHeader = showHeader;
    if (showHeader == YES) {
        __weak typeof(self)weakSelf = self;
        self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf loadEndView:NO];
            if (weakSelf.mjRefreshDataBlock) {
                weakSelf.mjRefreshDataBlock();
            }
            if (weakSelf.mjDelegate && [weakSelf.mjDelegate respondsToSelector:@selector(MjReloadAction)]) {
                [weakSelf.mjDelegate MjReloadAction];
            }
        }];
    }
    else{
        self.mj_header =(MJRefreshNormalHeader *)[[UIView alloc]init];
    }
}
-(void)setShowFooter:(BOOL)showFooter{
    _showFooter = showFooter;
    if (showFooter == YES) {
        __weak typeof(self)weakSelf = self;
        self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            if (weakSelf.mjLoadingMoreDataBlock) {
                weakSelf.mjLoadingMoreDataBlock();
            }
            if (weakSelf.mjDelegate && [weakSelf.mjDelegate respondsToSelector:@selector(MjRoadMoreAction)]) {
                [weakSelf.mjDelegate MjRoadMoreAction];
            }
        }];
    }
    else{
        self.mj_footer = (MJRefreshBackNormalFooter *)[[UIView alloc]init];
    }
}

-(void)endRefresh{
    if ([self.mj_header isKindOfClass:[MJRefreshNormalHeader class]]) {
        if (self.mj_header.isRefreshing) {
            [self.mj_header endRefreshing];
        }
    }
    if ([self.mj_footer isKindOfClass:[MJRefreshAutoNormalFooter class]]) {
        if (self.mj_footer.isRefreshing  && !isFooterHiiden) {
            [self.mj_footer endRefreshing];
        }
    }

}
/// 没有数据显示的view
-(void)loadEndView:(BOOL)is_Hidden{
    isFooterHiiden = is_Hidden;
    if ([self.mj_footer isKindOfClass:[MJRefreshAutoNormalFooter class]]) {
        if(is_Hidden == YES){
            [self.mj_footer endRefreshingWithNoMoreData];
        }
        else{
            [self.mj_footer resetNoMoreData];
        }
    }


}

@end
